package 华为OD机试真题2023.进阶题;

import utils.MyUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class HJ26_寻找相似单词 {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner in = new Scanner(new File("src/main/java/华为OD机试真题2023/进阶题/input/26.txt"));
        MyUtil.START();
        //        Solution(in);
        Solution2(in);
        MyUtil.END();
    }

    static void Solution(Scanner in) {
        int N = in.nextInt();
        in.nextLine(); // 吸收回车
        String[] list = new String[N];
        for (int i = 0; i < N; i++) {
            list[i] = in.nextLine();
        }
        char[] find = in.nextLine().toCharArray();
        Arrays.sort(find);
        TreeSet<String> result = new TreeSet<>();
        for (int i = 0; i < list.length; i++) {
            String tmp = list[i];
            char[] tmpCharArray = tmp.toCharArray();
            Arrays.sort(tmpCharArray);
            if (isValid(find, tmpCharArray)) {
                result.add(tmp);
            }
        }

        if (result.size() == 0) {
            System.out.println("null");
            return;
        }
        for (String str : result) {
            System.out.print(str + " ");
        }
    }

    static boolean isValid(char[] a, char[] b) {
        if (a.length != b.length) {
            return false;
        }
        for (int i = 0; i < a.length; i++) {
            if (a[i] != b[i]) {
                return false;
            }
        }
        return true;
    }

    static void Solution2(Scanner in) {
        int n = in.nextInt();
        String[] str = new String[n];
        String s;
        for (int i = 0; i < n; i++) {
            str[i] = in.next();
        }
        s = in.next();

        TreeSet<String> set = new TreeSet<>();
        for (int i = 0; i < str.length; i++) {
            if (isXiangShi(s, str[i])) {
                set.add(str[i]);
            }
        }

        if (set.size() == 0) {
            System.out.println("null");
        } else {
            for (String c : set) {
                System.out.print(c + " ");
            }
        }
    }

    public static boolean isXiangShi(String a, String b) {
        if (a.length() != b.length()) {
            return false;
        }
        char[] s1 = a.toCharArray();
        char[] s2 = b.toCharArray();
        Arrays.sort(s1);
        Arrays.sort(s2);
        for (int i = 0; i < s1.length; i++) {
            if (s1[i] != s2[i]) {
                return false;
            }
        }
        return true;
    }
}
